package com.leyou.item.controller;

import com.leyou.common.utils.PageResult;
import com.leyou.item.dto.CartDto;
import com.leyou.item.pojo.Sku;
import com.leyou.item.pojo.Spu;
import com.leyou.item.pojo.SpuBo;
import com.leyou.item.pojo.SpuDetail;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    /**
     * 分页查询SPU
     * @param page
     * @param rows
     * @param key
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuBo>> findSpuByPage(@RequestParam(value = "page",defaultValue = "1")  Integer page,@RequestParam(value = "rows",defaultValue = "5") Integer rows,
                                                           @RequestParam(value = "key",required = false)String key,
                                                           @RequestParam(value = "saleable", required = false) Boolean saleable) {
        PageResult<SpuBo> result = this.goodsService.querySpuByPageAndSort(page, rows,saleable, key);

        if (result == null || result.getItems().size() == 0) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return   ResponseEntity.ok(result);
    }


    /**
     * 根据spuId查询detail
     *
     * @param spuId
     * @return
     */
    @GetMapping("/spu/detail/{spuId}")
    public ResponseEntity<SpuDetail> querySpuDetailBySpuId(@PathVariable("spuId") Long spuId) {
        SpuDetail detail = this.goodsService.querySpuDetailById(spuId);
        if (detail == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(detail);
    }
    /**
     * 根据spuId查询商品详情
     * @param id
     * @return
     */
    @GetMapping("sku/list")
    public  ResponseEntity<List<Sku>> querySkuBySpuId(@RequestParam("id") Long id){
        List<Sku> skus =  this.goodsService.querySkuBySpuId(id);
        if (skus==null && skus.size()==0){
            return  new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return  ResponseEntity.ok(skus);
    }
    /**
     * 根据sku的id集合查询sku集合
     * @param ids
     * @return
     */
    @GetMapping("sku/list/ids")
    public ResponseEntity<List<Sku>> querySkuByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(goodsService.querySkuByIds(ids));
    }

    /**
     * @author:zty
     * @date: 2019年01月17日 16:55:51
     * @description 根据sid查询Sku
     * @method  querySkusBySpuId
     * @param sid
     * @return org.springframework.http.ResponseEntity<java.util.List<com.leyou.item.pojo.Sku>>
     */
    @GetMapping("sku/list/{sid}")
    public ResponseEntity<List<Sku>> querySkusBySpuId(@PathVariable("sid") Long sid){
        if (null == sid){
            return ResponseEntity.badRequest().build();
        }
        List<Sku> skus = goodsService.querySkuBySpuId(sid);
        if (!CollectionUtils.isEmpty(skus)){
            return ResponseEntity.ok(skus);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    /**
     * 根据spuId查询spu信息
     * @param spuId
     * @return
     */
    @GetMapping("spu/{spuId}")
    public ResponseEntity<Spu> querySpuBySpuId(@PathVariable("spuId") Long spuId) {
        return ResponseEntity.ok(goodsService.querySpuBySpuId(spuId));
    }
    /**
     * @author:zty
     * @date: 2019年01月17日 17:25:27
     * @description 根据sid查询sku
     * @method  querySkuBySkuId
     * @param id
     * @return org.springframework.http.ResponseEntity<com.leyou.item.pojo.Sku>
     */
    @GetMapping("sku/{id}")
    public ResponseEntity<Sku> querySkuBySkuId(@PathVariable("id")Long id){
        if (null == id){
            return ResponseEntity.badRequest().build();
        }
        Sku sku = goodsService.querySkuBySkuId(id);
        if (null == id){
            return ResponseEntity.noContent().build();
        }
        return ResponseEntity.ok(sku);

    }
    /**
     * 查询所有商品的spuId集合
     * @return
     */
    @GetMapping("spu")
    public ResponseEntity<List<Sku>> queryAllSpuId() {
        List<Sku> skus = this.goodsService.queryAllSpuId();
        if (skus==null && skus.size()==0){
            return  new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return  ResponseEntity.ok(skus);
    }

    /**
     * 新增商品
     * @param spu
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody Spu spu) {
        try {
            this.goodsService.save(spu);
            return new ResponseEntity<>(HttpStatus.CREATED);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    /**
     * 删除商品
     * @param spuId
     * @return
     */
    @DeleteMapping("/delete/{spuId}")
    public ResponseEntity<Void> deleteGoods(@PathVariable("spuId") Long spuId) {
        goodsService.deleteGoods(spuId);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }




    /**
     * 修改商品
     * @param spu
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateGoods(@RequestBody SpuBo spu) {
        try {
            this.goodsService.update(spu);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 上架商品
     * @param spuId
     * @return
     */
    @PutMapping("/goods/on/{spuId}")
    public ResponseEntity<Void> onSaleGoods(@PathVariable("spuId") Long spuId) {
        goodsService.onSaleGoods(spuId);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 下架商品
     * @param spuId
     * @return
     */
    @PutMapping("/goods/off/{spuId}")
    public ResponseEntity<Void> offSaleGoods(@PathVariable("spuId") Long spuId) {
        goodsService.offSaleGoods(spuId);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    /**
     * 减库存
     * @param cartDtos
     */
    @PostMapping("stock/decrease")
    public ResponseEntity<Void> decreaseStock(@RequestBody List<CartDto> cartDtos){
        goodsService.decreaseStock(cartDtos);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 订单支付超时,取消订单后把减掉的库存加回来
     * @param
     * @param
     */
    @PostMapping("stock/crease")
    public ResponseEntity<Void> creaseStock(@RequestBody Map<Long, Integer> map ){
        goodsService.creaseStock(map);
        return  ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}
